<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../../js/boot.js"></script>
</head>

<body>
    <div class="layui-fluid">
        <div class="flexrow">
            <div class="layui-panel list-container">
                <div class="list-search-container">
                    <button class="layui-btn layui-btn-sm" onclick="HadnleExport()"><i class="layui-icon layui-icon-export layui-font-12"></i>导出物理结构</button>
                    <button class="layui-btn layui-btn-sm" onclick="HandleImport()"><i class="layui-icon layui-icon-upload-circle layui-font-12"></i>导入物理结构</button>
                    <button class="layui-btn layui-btn-sm" onclick="HandleAdd()"><i class="layui-icon layui-icon-add-1 layui-font-12"></i>同步表信息</button>
                    <button class="layui-btn layui-btn-sm" onclick="HandleGenCode()"><i class="layui-icon layui-icon-fonts-code layui-font-12"></i>生成代码</button>
                    <div class="list-search-item">
                        <span>表名称：</span>
                        <input id="table_name" autocomplete="off" class="layui-input">
                    </div>
                    <div class="list-search-item">
                        <span>表描述：</span>
                        <input id="table_description" autocomplete="off" class="layui-input">
                    </div>
                    <button class="layui-btn layui-btn-sm" style="margin-left:5px" onclick="BindTableData()"><i class="layui-icon layui-icon-search layui-font-12"></i>搜索</button>
                </div>
                <div style="display: none">
                    <input type="file" name="file" id="file" onchange="uploadXml()" accept=".xml" />
                </div>
                <table class="layui-table" lay-data="{id: 'datagrid'}" lay-filter="datagrid">
                    <thead>
                        <tr>
                            <th lay-data="{type:'checkbox'}"></th>
                            <th lay-data="{field:'table_name'}">表名称</th>
                            <th lay-data="{field:'table_description'}">表描述</th>
                            <th lay-data="{field:'package_name', width:300, align:'left'}">包名</th>
                            <th lay-data="{field:'route_path', width:300, align:'left'}">路由</th>
                            <th lay-data="{fixed: 'right', width:400, align:'center', toolbar: '#op'}">操作</th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
</body>
<script type="text/html" id="op">
    <button class="layui-btn layui-btn-sm" lay-event="HandleEdit"><i class="layui-icon layui-icon-edit layui-font-12"></i>编辑</button>
    <button class="layui-btn layui-btn-sm" lay-event="HandleColumnEdit"><i class="layui-icon layui-icon-set-fill layui-font-12"></i>列设置</button>
    <button class="layui-btn layui-btn-sm" lay-event="HandleSync"><i class="layui-icon layui-icon-release layui-font-12"></i>同步列信息</button>
    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="HandleDelete"><i class="layui-icon layui-icon-delete layui-font-12"></i>删除</button>
</script>
<script>
    $(function() {
        BindTableData();
    })

    function BindTableData() {
        var param = {
            table_name: $("#table_name").val(),
            table_description: $("#table_description").val(),
        }
        BindTable("datagrid", "system/codegen/table/list", param);
    }


    function HandleAdd() {
        OpenDialog('同步表信息', 'system/codegen/tableimport_list.html', 900, 700, function() {
            BindTableData();
        })
    }

    function HandleEdit(RowData) {
        OpenDialog('修改表信息', 'system/codegen/table_edit.html?row_id=' + RowData.row_id, 800, 600, function() {
            BindTableData();
        })
    }

    function HandleColumnEdit(RowData) {
        OpenDialog('列管理', 'system/codegen/colum_list.html?table_id=' + RowData.row_id, 1366, 768, function() {
            BindTableData();
        })
    }

    function HandleSync(RowData) {
        var param = {
            table_name: RowData.table_name
        }
        StartLoading("同步中...")
        HttpPost(JSON.stringify(param), 'system/codegen/syncTable', function(result) {
            CloseLoading()
            if (result.code == 0) {
                OpenSuccessMessage("同步成功")
                BindTableData();
            } else {
                OpenFail(result.msg);
            }
        })
    }

    function HandleDelete(RowData) {
        OpenConfirm('确认删除？', function() {
            var param = {
                row_id: RowData.row_id
            }
            HttpPost(JSON.stringify(param), 'system/codegen/table/delete', function(result) {
                if (result.code == 0) {
                    OpenSuccessMessage("删除成功")
                    BindTableData();
                } else {
                    OpenFail(result.msg);
                }
            })
        })
    }

    function HandleGenCode() {
        var tableData = GetCheckData();
        if (tableData.length == 0) {
            OpenWarning("未选择数据")
            return;
        }
        var tableNameList = []
        for (var i = 0; i < tableData.length; i++) {
            tableNameList.push(tableData[i].table_name)
        }
        StartLoading("生成中...")
        HttpPost(JSON.stringify(tableNameList), 'system/codegen/createCode', function(result) {
            CloseLoading()
            if (result.code == 0) {
                window.top.open(result.data.obj);
            } else {
                OpenFail(result.msg);
            }
        })
    }

    function HadnleExport() {
        var tableData = GetCheckData();
        if (tableData.length == 0) {
            OpenWarning("未选择导出的表")
            return;
        }
        var tableNameList = []
        for (var i = 0; i < tableData.length; i++) {
            tableNameList.push(tableData[i].table_name)
        }
        StartLoading("导出中...")
        HttpPost(JSON.stringify(tableNameList), 'system/codegen/exportData', function(result) {
            CloseLoading()
            if (result.code == 0) {
                window.top.open(GetApiPath() + "/system/common/attach/readExportXmlFile?fileid=" + result.data.fileid + "&token=" + GetTokenID())
            } else {
                OpenFail(result.msg);
            }
        })
    }

    function HandleImport() {
        document.getElementById("file").click();
    }

    function uploadXml() {
        StartLoading("导入中");
        var formData = new FormData();
        formData.append('file', document.getElementById('file').files[0]);
        HttpForm(formData, 'system/codegen/importData', function(result, status) {
            CloseLoading();
            if (result.code == 0) {
                OpenSuccess('导入成功', function() {
                    BindTableData();
                })
            } else {
                OpenFail(result.msg);
            }
            document.getElementById("file").value = "";
        })
    }
</script>

</html>